<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>修改优惠券</title>
    <style>
        div {
            position: absolute;
            top: 30%;
            left: 50%;
            transform: translate(-50%, -50%);
            text-align: center;
        }

        table, tr, th, td {
            border: 1px solid #5fbdcb;
        }

        table {
            border-collapse: collapse;
        }
    </style>
</head>
<body>
<div>
    <form method="post" action="${pageContext.request.contextPath}/dish.do">
        请选择你要修改的：<label>
        <select>
            <option value="value">金额</option>
            <option value="min">最低消费</option>
            <option value="validity">有效时间</option>
        </select><br>
    </label>
    </form>
    <input type="button" id="button" value="提交"><br>
    <a id="a">返回</a>
</div>
<script src="https://lib.sinaapp.com/js/jquery/2.0.2/jquery-2.0.2.min.js"></script>
<script>
    function getParam(url, name) {
        try {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = url.split('?')[1].match(reg);
            if (r != null) {
                return r[2];
            }
            return "";
        } catch (e) {
            return "";
        }
    }

    let url = window.location.href;
    let storeId = getParam(url, 'storeId');
    let couponId = getParam(url, 'couponId');
    let selectElement = document.querySelector('select');
    let form = document.querySelector('form');
    let input = document.createElement('input');
    input.type = 'number';
    input.min = '0';
    input.name = 'value';
    input.step = '0.1';
    input.placeholder = '请输入面值';
    form.appendChild(input);
    selectElement.onchange = function () {
        if (selectElement.value === 'value') {
            input.step = '0.1';
            input.placeholder = '请输入面值';
        }
        if (selectElement.value === 'min') {
            input.step = '0.1';
            input.placeholder = '请输入最低消费';
        }
        if (selectElement.value === 'validity') {
            input.step = '1';
            input.placeholder = '请输入有效时间';
        }
    }
    $(function () {
        let a = document.querySelector("#a");
        a.href = 'SellerCoupon.jsp?storeId=' + storeId;
        $(':button').on('click', function () {
            $.ajax({
                url: "http://localhost:8080/coupon.do",
                type: "post",
                data: {
                    field: selectElement.value,
                    value: input.value,
                    storeId: storeId,
                    couponId: couponId,
                    method: 'modifyCoupon'
                },
                dataType: "text",
                success: function (data) {
                    if (data === 'ok') {
                        alert("修改成功");
                    } else {
                        alert("修改失败");
                    }
                }
            })
        })
    })
</script>
</body>
</html>
